package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.BookClassification;
import com.example.demo.entity.Top100InternetCompany;
import com.example.demo.entity.result.NameValue;
import com.example.demo.mapper.Top100InternetCompanyMapper;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/***
 ** Create by Wedream on 2021/7/16
 */
@RestController
@RequestMapping("/top100InternetCompany")
public class Top100InternetCompanyController {
    
    @Resource
    Top100InternetCompanyMapper top100InternetCompanyMapper;

    @PostMapping  //增
    public Result<?> save(@RequestBody Top100InternetCompany top100InternetCompany) {
        top100InternetCompanyMapper.insert(top100InternetCompany);
        return Result.success();
    }

    @DeleteMapping("/{id}") //删
    public Result<?> delete(@PathVariable long id) {
        top100InternetCompanyMapper.deleteById(id);
        return Result.success();
    }

    @GetMapping("/all") //查
    public Result<?> findAllPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "100") Integer pageSize,
                              @RequestParam(defaultValue = "",required = false) String search) {

        LambdaQueryWrapper<Top100InternetCompany> wrapper = Wrappers.<Top100InternetCompany>lambdaQuery();
        if(StrUtil.isNotBlank(search)) {
            wrapper.like(Top100InternetCompany::getCompany,search);
        }
        Page<Top100InternetCompany> top100InternetCompanyPage = top100InternetCompanyMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
//        top100InternetCompanyPage.setRecords(nv100);
        List<Top100InternetCompany> records = top100InternetCompanyPage.getRecords();
        Collections.sort(records);
        top100InternetCompanyPage.setRecords(records);
        return Result.success(top100InternetCompanyPage);
    }

    @GetMapping("/unsorted") //查
    public Result<?> findUnsortedPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                 @RequestParam(defaultValue = "100") Integer pageSize,
                                 @RequestParam(defaultValue = "",required = false) String search) {

        LambdaQueryWrapper<Top100InternetCompany> wrapper = Wrappers.<Top100InternetCompany>lambdaQuery();
        if(StrUtil.isNotBlank(search)) {
            wrapper.like(Top100InternetCompany::getCompany,search);
        }
        Page<Top100InternetCompany> top100InternetCompanyPage = top100InternetCompanyMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);

        return Result.success(top100InternetCompanyPage);
    }

    @GetMapping //查
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "100") Integer pageSize,
                              @RequestParam(defaultValue = "",required = false) String search) {

        LambdaQueryWrapper<Top100InternetCompany> wrapper = Wrappers.<Top100InternetCompany>lambdaQuery();
        if(StrUtil.isNotBlank(search)) {
           wrapper.like(Top100InternetCompany::getCompany,search);
        }

        Page<Top100InternetCompany> top100InternetCompanyPage = top100InternetCompanyMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        List<Top100InternetCompany> top100 = top100InternetCompanyPage.getRecords(); //查询的（100条）记录
        List<NameValue> nv100 = new ArrayList<>();
//        System.out.println(top100);
        Map<String,Integer> mp = new HashMap(); // key:省份->value:100强公司数量

        for (Top100InternetCompany top: top100) {
            // 计算该省份的100强公司数量
            if(mp.containsKey(top.getProvince())) {
                mp.put(top.getProvince(),mp.get(top.getProvince())+1);
            } else {
                mp.put(top.getProvince(),1);
            }
        }

        // 遍历每个省份的100强公司数量，装进nv100列表里面
        for(Map.Entry<String, Integer> entry : mp.entrySet()){
            // System.out.println("foreachEntry : key :" + entry.getKey() + "---> value :"+entry.getValue());
            nv100.add(new NameValue(entry.getKey(),entry.getValue()));
        }
//        System.out.println(nv100);

        Page<NameValue> nameValuePage = new Page();  // 创建NameValue类型的Page,前端需要该类型的数据
        nameValuePage.setRecords(nv100);
        nameValuePage.setTotal(top100InternetCompanyPage.getTotal());
        nameValuePage.setSize(top100InternetCompanyPage.getSize());
        nameValuePage.setCurrent(top100InternetCompanyPage.getCurrent());
        nameValuePage.setOrders(top100InternetCompanyPage.getOrders());
        nameValuePage.setPages(top100InternetCompanyPage.getPages());

//        top100InternetCompanyPage.setRecords(nv100);
        return Result.success(nameValuePage);
    }

    // 导出Excel
    @ResponseExcel(name = "top100InternetCompany", sheet = "top100InternetCompany")
    @GetMapping("/downloadExcel")
    public List<Top100InternetCompany> DownloadExcel(@RequestParam(defaultValue = "1") Integer pageNum,
                                                  @RequestParam(defaultValue = "1000") Integer pageSize,
                                                  @RequestParam(defaultValue = "",required = false) String search) {

        LambdaQueryWrapper<Top100InternetCompany> wrapper = Wrappers.<Top100InternetCompany>lambdaQuery();
        if(StrUtil.isNotBlank(search)) {
            wrapper.like(Top100InternetCompany::getCompany,search);
        }
        Page<Top100InternetCompany> top100InternetCompanyPage = top100InternetCompanyMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        List<Top100InternetCompany> records = top100InternetCompanyPage.getRecords();
        return records;
    }

    @PutMapping //改
    public Result<?> update(@RequestBody Top100InternetCompany top100InternetCompany) {
        top100InternetCompanyMapper.updateById(top100InternetCompany);
        return Result.success();
    }
}
